﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LevelTool
{
  public partial class Form1 : Form
  {
    string[,] matrix;

    private int currentX = 0;
    private int currentY = 0;

    public Form1() {
      InitializeComponent();
      pnl.Visible = pnl2.Visible = false;
      UpdateFieldSet();
    }

    private void UpdateFieldSet() {
      groupBox1.Left = this.Width - groupBox1.Width - 25;
      groupBox2.Left = this.Width - groupBox2.Width - 25;
      c_btnCopy.Left = this.Width - c_btnCopy.Width - 25;
      c_txtResult.Width = this.Width - (this.Padding.Left + this.Padding.Right + c_txtResult.Margin.Left + c_txtResult.Margin.Right + c_txtResult.Left);
    }

    private void btn_set_Click(object sender, EventArgs e) {
      var rows = (int)c_nudRows.Value;
      var cols = (int)c_nudCols.Value;
      var configuration = new CthulhuDomain.GameEntities.MatrixConfiguration();
      matrix = new string[rows, cols];

      for (var i = 0; i < rows; i++) {
        for (var j = 0; j < cols; j++) {
          var btn = new Button { Text = string.Format("{0}-{1}", i, j), Width = 30, Height = 30, Top = 30 * j, Left = 30 * i };
          btn.Click += GridButton_Click;
          btn.MouseEnter += GridButton_Hover;
          btn.MouseLeave += GridButton_Out;
          pnl.Controls.Add(btn);

          var txt = new TextBox {
            Width = 30,
            Height = 30,
            Top = 30 * j,
            Left = 30 * i,
            Name = string.Format("txt{0}_{1}", i, j),
            ReadOnly = true,
            Text = "{0-0}",
            BackColor = Color.Black,
            ForeColor = Color.White
          };
          pnl2.Controls.Add(txt);

          matrix[i, j] = "{0-0}";
        }
      }

      pnl2.Left = pnl.Left + pnl.Width + 5;
      pnl2.Top = pnl.Top;
      pnl.Visible = pnl2.Visible = true;

    }

    private void GridButton_Click(object sender, EventArgs e) {
      c_txt_currentX.Text = ((Button)sender).Text.Split('-')[0];
      c_txt_currentY.Text = ((Button)sender).Text.Split('-')[1];
      currentX = int.Parse(c_txt_currentX.Text);
      currentY = int.Parse(c_txt_currentY.Text);

      var txtBox = (TextBox)pnl2.Controls.Find(string.Format("txt{0}_{1}", currentX, currentY), false)[0];
      txtBox.BackColor = Color.Orange;
      CurrentColor = Color.Orange;
      txtBox.ForeColor = Color.Black;

    }
    private void GridButton_Hover(object sender, EventArgs e) {
      var txtBox = (TextBox)pnl2.Controls.Find(string.Format("txt{0}_{1}", ((Button)sender).Text.Split('-')[0], ((Button)sender).Text.Split('-')[1]), false)[0];
      CurrentColor = txtBox.BackColor; 
      txtBox.BackColor = Color.Chartreuse;
      
    }

    protected Color CurrentColor { get; set; }

    private void GridButton_Out(object sender, EventArgs e) {
      var txtBox = (TextBox)pnl2.Controls.Find(string.Format("txt{0}_{1}", ((Button)sender).Text.Split('-')[0], ((Button)sender).Text.Split('-')[1]), false)[0];
      txtBox.BackColor = CurrentColor;
    }

    private void setMatrixElement_Click(object sender, EventArgs e) {
      var value = string.Format("{2}{0}-{1}{3}", c_nupType.Value, c_nupLifePoints.Value, "{", "}");
      matrix[currentX, currentY] = value;
      var currentTxt = (TextBox)pnl2.Controls.Find(string.Format("txt{0}_{1}", currentX, currentY), false)[0];
      currentTxt.Text = value;

      currentTxt.BackColor = ColorByLifePoints(c_nupLifePoints.Value);
      currentTxt.Update();
    }

    private static Color ColorByLifePoints(decimal value) {
      var color = Color.Black;
      switch ((int)value) {
        case 1: color = Color.White; break;
        case 2: color = Color.Yellow; break;
        case 3: color = Color.Red; break;
      }
      return color;
    }

    private void UpdateMatrix_Click(object sender, EventArgs e) {
      var cfg = new StringBuilder();
      for (var i = 0; i < matrix.GetLength(1); i++) {
        for (var j = 0; j < matrix.GetLength(0); j++) {
          cfg.Append(matrix[j, i].Replace("-",":"));
          cfg.Append(j == matrix.GetLength(0) - 1 ? ";" : ",");
        }
      }


      c_txtResult.Text = cfg.ToString();
      c_txtResult.Update();
    }

    private void c_btnCopy_Click(object sender, EventArgs e) {
      c_txtResult.SelectAll();
      Clipboard.SetData(DataFormats.Text, c_txtResult.Text);
    }

    private void Form1_ResizeEnd(object sender, EventArgs e) {
      UpdateFieldSet();
    }

  }
}
